<?php
    $q = $_GET["q"];
    $a = mysqli_connect("localhost","root","gala123456");
    $db = mysqli_select_db($a,"sheep");
    $sql = "SELECT `business_sheepID` FROM `business` WHERE `business_name` = '$q'";
    $query = mysqli_query($a,$sql);
    $sheep_url = array();
    $sheep_class = array();
    $sheep_birthday = array();
    $sheep_weight = array();
    $sheepID = array();
    while($row = mysqli_fetch_row($query)){
        foreach ($row as $nei) {
            $new_nei = explode("-",$nei);
            for ($i = 0;$i < count($new_nei);$i++) {
                $sql2 = "SELECT `sheep_select`,`sheep_url`,`sheep_class`,`sheep_birthday`,`sheep_weight` FROM `sheep` WHERE `sheepID` = '".$new_nei[$i]."'";
                $query2 = mysqli_query($a,$sql2);
                while ($row2 = mysqli_fetch_row($query2)) {
                    $ding = 0;
                    $qu = 0;
                    foreach ($row2 as $nei2) {
                        if ($ding == 0) {
                            if ($nei2 == null) {
                                $qu = 0;
                            }else{
                                $qu = 1;
                            }
                        }else if ($ding == 1 && $qu == 0) {
                            $new_sheep_url = explode("-",$nei2);
                            array_push($sheep_url,"https://726y7113a8.zicp.fun/API/sheep_img/".$new_sheep_url[0]);
                        }else if ($ding == 2 && $qu == 0) {
                            array_push($sheep_class,$nei2);
                        }else if ($ding == 3 && $qu == 0) {
                            array_push($sheep_birthday,$nei2);
                        }else if ($ding == 4 && $qu == 0) {
                            array_push($sheep_weight,$nei2);
                            array_push($sheepID,$new_nei[$i]);
                        }
                        $ding++;
                    }
                }
            }
        }
    }
    $arr = array(
        "sheepID" => $sheepID,
        "sheep_url" => $sheep_url,
        "sheep_class" => $sheep_class,
        "sheep_birthday" => $sheep_birthday,
        "sheep_weight" => $sheep_weight
    );
    // var_dump($arr);
    echo json_encode($arr);
?>